<?php
/**
 * Created by IntelliJ IDEA.
 * User: m1573
 * Date: 2022/7/23
 * Time: 19:18
 */
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="static/css/bootstrap.min.css" rel="stylesheet">
    <link href="static/css/all.css" rel="stylesheet">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <link rel="stylesheet" href="static/css/bootstrap.min.css" type="text/css">
    <script type="text/javascript" src="static/js/jquery.js"></script>
    <script type="text/javascript" src="static/js/bootstrap.js"></script>

</head>
<?php
session_start();
$qids = $_GET["qids"];//?qid=1&a=2  ?qid[]=1&qid[]=2
$answers = $_GET["answers"];
include("include/util.inc.php");
include("include/header.php");
function cmpAnswer($real , $mine){
    $real = trim($real);
    if(strcmp($real, $mine)==0){
        return true;
    }
    return false;
}

function cmp_getScore($reals,$mines){
    $final_score = 0;
    $size = count($reals);
    for($i = 0; $i < $size; $i= $i + 1){
        if(cmpAnswer($reals[$i],$mines[$i])){
            $final_score = $final_score + 1;
        }
    }
    return $final_score . "/" . $size;
}
function get_table($PDO){
    $sql = "select * from question where ";
    global $qids;
    foreach($qids as $qid){
        $sql = $sql . "qid = " . $qid . " or ";
    }
    $sql  = $sql . "0  = 1";

    $res = $PDO->query($sql);
    $table = [];

    while($row = $res->fetch()){
        array_push($table, $row);
    }
    return $table;
}

function parse_real_answer($table){
    $real_answer = [];
    for($i = 0; $i < count($table); $i = $i + 1){
        array_push($real_answer,$table[$i]["answer_part"]);
    }
    return $real_answer;
}

function format_table($table,$mines){
    $html = "";
    for($i = 0;$i < count($table); $i = $i + 1){
        if(cmpAnswer($table[$i]["answer_part"],$mines[$i]) === false){
            $html = $html."<tr>
                   <td>{$table[$i]["question_part"]}</td>
                   <td>{$table[$i]["category"]}</td>
                   <td>{$table[$i]["level_part"]}</td>
                   <td>{$table[$i]["ispublic"]}</td>
                   <td>{$mines[$i]}</td>
                   <td>{$table[$i]["answer_part"]}</td>
                   
                            </tr>";
        }

    }

    return $html;
}

$table = get_table($PDO);
$real_answer = parse_real_answer($table);
$formatted_table = format_table($table,$answers);
$score = cmp_getScore($real_answer,$answers);


?>

<body class="container bg">
<div class="container ">
    <div class="row mb-lg-5" id="score">
        <p class="h1 text-center">The Score is <?=$score?></p>
    </div>
    <div id="result" class="row col-xl-8 offset-xl-2 bg-white rounded">
        <table class="table table-bordered text-center">
            <thead>
            <tr>
                <th scope="col">question</th>
                <th scope="col">category</th>
                <th scope="col">level</th>
                <th scope="col">mode</th>
                <th scope="col">my answer</th>
                <th scope="col">real answer</th>
                
            </tr>
            </thead>
            <tbody>
            <?=$formatted_table?>

            </tbody>
        </table>
    </div>
</div>

</body>
</html>

<script type="text/javascript" src="static/js/simpleAjax.js"></script>
<script>

</script>
